package app.fugema.shop.main.evaluate.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.databinding.BaseTitleToolbarBinding;
import com.base.common.model.bean.ADInfo;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.ColorUtils;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.EvaluateAcItemBinding;
import app.fugema.shop.databinding.EvaluateGoodsAcBinding;
import app.fugema.shop.main.evaluate.adapter.GoodsCommentAdapter;
import app.fugema.shop.main.evaluate.vm.EvaluateVM;
import app.fugema.shop.main.evaluate.model.CommentListResult;

import java.util.ArrayList;
import java.util.List;

public class EvaluateGoodsActivity extends BaseTitleActivity<EvaluateGoodsAcBinding, EvaluateVM> implements OnGetDataListener {
    //商品评论列表

    public static void start(Context context, int goodsId)
    {
        Intent starter = new Intent(context, EvaluateGoodsActivity.class);
        starter.putExtra("prodId", goodsId);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evaluate_goods_ac);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    public void initToolBar(BaseTitleToolbarBinding mTitleBinding)
    {
        super.initToolBar(mTitleBinding);
        //mTitleBinding.clToolbar.setBackgroundColor(ColorUtils.getColor(R.color.C_FFFFFF));
    }

    private SingleRecyclerAdapter<ADInfo, EvaluateAcItemBinding> adapterTop;

    private GoodsCommentAdapter adapterEvaluate;
    private int prodId;
    //评论类型： 0：所有 默认 1：好评 2：中评 3：差评 4：有图 5：追加
    private int[] type = {0,1,2,3,4,5};
    //当前的是什么类型
    private int currItemType = 0;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("评价",R.color.white);
        adapterTop = new SingleRecyclerAdapter<ADInfo, EvaluateAcItemBinding>() {
            @Override
            public int getLayoutId()
            {
                return R.layout.evaluate_ac_item;
            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, ADInfo bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);
                if (currItemType == bean.getImageType()) return;
                setSelectPosition(position);
                currItemType = bean.getImageType();
                onGetData(1);
            }
        };
        adapterTop.setRadio(true);
        binding.rvRecyclerViewTop.setAdapter(adapterTop);

        adapterEvaluate = new GoodsCommentAdapter();
        binding.rvRecyclerView.setAdapter(adapterEvaluate);

        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        prodId = intent.getIntExtra("prodId", 0);

        List<ADInfo> list = new ArrayList<>();
        for (int i : type)
        {
            ADInfo adInfo = new ADInfo();
            adInfo.setImageType(i);
            if (currItemType == i)
            {
                adInfo.setSelected(true);
            }
            switch (i)
            {
                case 0:
                    adInfo.setImageName("全部");
                    break;
                case 1:
                    adInfo.setImageName("好评");
                    break;
                case 2:
                    adInfo.setImageName("中评");
                    break;
                case 3:
                    adInfo.setImageName("差评");
                    break;
                case 4:
                    adInfo.setImageName("有图");
                    break;
                case 5:
                    adInfo.setImageName("追加");
                    break;
            }
            list.add(adInfo);
        }
        adapterTop.setDataList(list);

        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {
        //commentType  评论类型： 0：所有 默认 1：好评 2：中评 3：差评 4：有图 5：追加
        viewModel.evaluatePageByProdId(prodId, pn, currItemType)
                 .observe(this, new BaseViewObserver<LiveDataWrapper<CommentListResult>>(viewUtils.getStateLayout())
                 {
                    @Override
                    protected void onSuccess(LiveDataWrapper<CommentListResult> data)
                    {
                        viewUtils.setDataListRefreshLayout(adapterEvaluate, pn, 10, data.data.getList());
                    }

                    @Override
                    public boolean isRefreshLayoutData()
                    {
                        return true;
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<CommentListResult> data)
                    {
                        return  (pn == 1 && UIUtils.isEmpty(data.data.getList()));
                    }
                 });
    }


}

